package com.pj.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.current.satoken.AuthConst;
import com.pj.project.model.mapper.SpGoodsDetailMapper;
import com.pj.project.model.mapper.SpGoodsSortMapper;
import com.pj.project.model.pojo.SpGoodsDetail;
import com.pj.project.model.pojo.SpGoodsSort;
import com.pj.project.model.pojo.SpGoodsWarehouse;
import com.pj.project.model.vo.SpGoodsDetailVO;
import com.pj.project.model.vo.SpGoodsSortVO;
import com.pj.project.service.SpGoodsDetailService;
import com.pj.project.service.SpGoodsSortService;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * GoodsSortController
 *
 * @author hanWu
 * 2023-01-26 11:36
 */
@RestController
@RequestMapping("/goods")
public class GoodsDetailController {

    @Resource
    SpGoodsDetailService goodsDetailService;

    @Resource
    SpGoodsDetailMapper goodsDetailMapper;

    @GetMapping("/success")
    public AjaxJson success() {
        return AjaxJson.getSuccess();
    }

    /**
     * 查看物品信息列表  todo  返回仓库 分类  名字  查询
     */
    @PostMapping("/list")
    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson list() {
        SoMap so = SoMap.getRequestSoMap();
        List<SpGoodsDetailVO> list = goodsDetailService.getList(so);
        return AjaxJson.getPageData(so.getDataCount(), list);
    }


    /**
     * 添加
     */
    @PostMapping
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson add(SpGoodsDetail goodsDetail) {
        goodsDetail.setCreateTime(new Date());
        goodsDetail.setUpdateTime(new Date());
        try {
            goodsDetailService.save(goodsDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.getError("添加失败");
        }

        return AjaxJson.getSuccess("添加成功", goodsDetail);

    }

    /**
     * 删除
     */
    @DeleteMapping
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson del(Integer id) {
        if (goodsDetailService.removeById(id)) {
            return AjaxJson.getSuccess("删除成功");
        } else {
            return AjaxJson.getError("删除失败");
        }
    }


    /**
     * 修改
     */
    @PostMapping("/update")
//    @SaCheckPermission(AuthConst.WAREHOUSE_LIST)
    public AjaxJson updateById(SpGoodsDetail goodsDetail) {
        boolean result = goodsDetailService.updateById(goodsDetail);
        if (result) {
            return AjaxJson.getSuccess("修改成功");
        } else {
            return AjaxJson.getError("修改失败");
        }
    }


}
